/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.start.controller;

import com.itgrail.grail.cqrs.command.CommandBus;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.pilot.app.command.cmo.AddDeptCmd;
import com.itgrail.pilot.app.command.cmo.DeleteDeptCmd;
import com.itgrail.pilot.app.command.cmo.UpdateDeptCmd;
import com.itgrail.pilot.app.command.cmo.query.DetailDeptCmd;
import com.itgrail.pilot.app.command.cmo.query.ListDeptTreeCmd;
import com.itgrail.pilot.app.command.cmo.query.PageDeptCmd;
import com.itgrail.pilot.app.command.cmo.query.SyncDeptTreeCmd;
import com.itgrail.pilot.infrastructure.tunnel.util.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xujin
 */
@RestController
@RequestMapping("/admin/dept")
@Api("部门相关操作")
public class DeptController {

    @Autowired
    private CommandBus commandBus;

    @PostMapping("/getDeptTree")
    @ApiOperation(value = "获取部门树")
    public ResultData getDeptTree(@RequestBody ListDeptTreeCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/synDingDept")
    @ApiOperation(value = "同步钉钉部门树")
    public ResultData synDingDept(@RequestBody SyncDeptTreeCmd syncDeptTreeCmd) {
        return commandBus.send(syncDeptTreeCmd);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新部门")
    public ResultData update(@RequestBody UpdateDeptCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增部门")
    public ResultData add(@RequestBody AddDeptCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除部门")
    public ResultData delete(@RequestBody DeleteDeptCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取部门列表分页")
    public ResultData page(@RequestBody PageDeptCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "获取部门详情")
    public ResultData detail(@RequestBody DetailDeptCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }
}
